package session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Miss Chen
 * @version 1.0
 * @date 2024年09月04日10:30
 */

@WebServlet("/count")
public class CountServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取session中count数据
        Integer count= (Integer) request.getSession().getAttribute("count");
        // 当第一次获取的时候，由于尚未存储，此时获取出来的数据为null
        if(count==null){
            count=1;//设置初始值
        }else{
            //如果不是第一次，则每次在原有的基础上加1
            count++;
        }
        request.getSession().setAttribute("count",count);
        request.getRequestDispatcher("/session/count.jsp").forward(request,response);
    }
}
